// Get Data & tsset it
use "SanctionsAndStockMarkets.dta", clear
tsset ccode yrmo

* Table 1
	* M1: Full Controls 
		xtreg StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP i.yrmo, fe vce(cluster ccode) 
		
		/*
		Estimating the LRM with Bewley Transformation:
		
		A constant X(t) and Y(t-1) are used as instruments to estimate the Bewley model. We first regress changes in y(t) 
		on lagged Y(t) contemporaneous values of the X and changes in X. Predicted values from this regression are included 
		in the Bewley model on the right hand side.

		In this specification, the coefficient on X(t) is the LRM and it has an	estimated standard error.
		*/
			xtivreg2 StMk ST ST2 ST3 IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dST dST2 dST3 dIntCon dCW dprelec dideoLef dopenk dlnTR dEXRat dCPI dlnGDP yrmoD* ///
			(dStMk = lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dST dST2 dST3 dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP) ///
			, fe robust cluster(ccode)
			
	* M2: Minimal Controls 
		xtreg StMk lStMk ST ST2 ST3 CPI lnGDP i.yrmo, fe vce(cluster ccode)
		
		* Estimating the LRM with Bewley Transformation		
			xtivreg2 StMk ST ST2 ST3 CPI lnGDP dST dST2 dST3 dEXRat dCPI dlnGDP yrmoD* ///
			(dStMk = lStMk ST ST2 ST3 EXRat CPI lnGDP dST dST2 dST3 dEXRat dCPI dlnGDP) ///
			, fe robust cluster(ccode)

* Table 2: Import, Export, and Financial Sanctions
	*M3: Import Sanctions
		xtreg StMk lStMk AnyIMPTty AnyIMPTty2 AnyIMPTty3 NotAnyIMPTty IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP ///
		i.yrmo, fe vce(cluster ccode) 
	
		*Estimating the LRM with Bewley Transformation:
			xtivreg2 StMk AnyIMPTty AnyIMPTty2 AnyIMPTty3 NotAnyIMPTty IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dAnyIMPTty dAnyIMPTty2 dAnyIMPTty3 dNotAnyIMPTty dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP yrmoD* ///
			(dStMk = lStMk AnyIMPTty AnyIMPTty2 AnyIMPTty3 NotAnyIMPTty IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dAnyIMPTty dAnyIMPTty2 dAnyIMPTty3 dNotAnyIMPTty dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP) ///
			, fe robust cluster(ccode)

	*M4: Export Sanctions
		xtreg StMk lStMk AnyExpTty AnyExpTty2 AnyExpTty3 NotAnyExpTty IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP ///
		i.yrmo, fe vce(cluster ccode) 
	
		*Estimating the LRM with Bewley Transformation:
			xtivreg2 StMk AnyExpTty AnyExpTty2 AnyExpTty3 NotAnyExpTty IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dAnyIMPTty dAnyIMPTty2 dAnyIMPTty3 dNotAnyIMPTty dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP yrmoD* ///
			(dStMk = lStMk AnyExpTty AnyExpTty2 AnyExpTty3 NotAnyExpTty IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dAnyExpTty dAnyExpTty2 dAnyExpTty3 NotAnyExpTty dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP) ///
			, fe robust cluster(ccode)

	*M5: Financial Sanctions
		xtreg StMk lStMk AnyFinSanTty AnyFinSanTty2 AnyFinSanTty3 NotAnyFinSanTty IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP ///
		i.yrmo, fe vce(cluster ccode) 
		
		*Estimating the LRM with Bewley Transformation:
			xtivreg2 StMk AnyFinSanTty AnyFinSanTty2 AnyFinSanTty3 NotAnyFinSanTty IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dAnyFinSanTty dAnyFinSanTty2 dAnyFinSanTty3 dNotAnyFinSanTty dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP yrmoD* ///
			(dStMk = lStMk AnyFinSanTty AnyFinSanTty2 AnyFinSanTty3 NotAnyFinSanTty IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
			dAnyFinSanTty dAnyFinSanTty2 dAnyFinSanTty3 dNotAnyFinSanTty dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP) ///
			, fe robust cluster(ccode)
	 
* Table 3: G20 Sanctions
	*M6: Full Controls
		xtreg StMk lStMk SG20T SG20T2 SG20T3 nonSG20T nonSG20T2 nonSG20T3 IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP i.yrmo, fe vce(cluster ccode) 
	
	*Estimating the LRM with Bewley Transformation:
		xtivreg2 StMk SG20T SG20T2 SG20T3 nonSG20T nonSG20T2 nonSG20T3 IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
		dSG20T dSG20T2 dSG20T3 dnonSG20T dnonSG20T2 dnonSG20T3 dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP yrmoD* ///
		(dStMk = lStMk SG20T SG20T2 SG20T3 nonSG20T nonSG20T2 nonSG20T3 IntCon CW prelec ideoLef openk  lnTR EXRat CPI lnGDP ///
		dSG20T dSG20T2 dSG20T3 dnonSG20T dnonSG20T2 dnonSG20T3 dIntCon dCW dprelec dideoLef dopenk  dlnTR dEXRat dCPI dlnGDP) ///
		, fe robust cluster(ccode)

	*M7: Minimal Controls
		xtreg StMk lStMk SG20T SG20T2 SG20T3 nonSG20T nonSG20T2 nonSG20T3 CPI lnGDP i.yrmo, fe vce(cluster ccode) 
	
	*Estimating the LRM with Bewley Transformation:
		xtivreg2 StMk SG20T SG20T2 SG20T3 nonSG20T nonSG20T2 nonSG20T3  CPI lnGDP ///
		dSG20T dSG20T2 dSG20T3 dnonSG20T dnonSG20T2 dnonSG20T3  dCPI dlnGDP yrmoD* ///
		(dStMk = lStMk SG20T SG20T2 SG20T3 nonSG20T nonSG20T2 nonSG20T3  CPI lnGDP ///
		dSG20T dSG20T2 dSG20T3 dnonSG20T dnonSG20T2 dnonSG20T3  dCPI dlnGDP) ///
		, fe robust cluster(ccode)

* figure 1
	xtreg StMk lStMk c.ST##c.ST##c.ST IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP i.yrmo, fe vce(cluster ccode)
	
	* Get the sample mean and std. dev
		codebook ST dStMk StMk if e(sample)==1 
	* Estimate the margins
		margins, at(ST=(0(1)12)) dydx(ST) vsquish		
	* Plot the margins
		marginsplot,addplot(hist ST if ST<12, ///
		bcolor(none) yaxis(2) yscale(alt) xlabel(0(1)12)) recastci(rline) ciopts(lpattern(dash)) yline(0) ///
		level(90) recast(line)  ///
		legend(off) ///
		scheme(s2mono) graphregion(fcolor(white)) ///
		xtitle(# Existing Sanctions Against Country) ///
		ytitle(Marg. Eff. of Sanctions on Changes in Market Capitalization, size(small)) ///
		title("") 
	
* figure 2
	xtreg dStMk lStMk c.ST##c.ST##c.ST IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP i.ccode i.yrmo, fe vce(cluster ccode)
	
	* Calculate the margins
		margins, at(ST=(0(1)12))
		
	* Plot the margins	
		marginsplot,addplot(hist ST if ST<12, ///
		bcolor(none) yaxis(2) yscale(alt) xlabel(0(1)12)) recastci(rline) ciopts(lpattern(dash)) yline(0) ///
		level(90) recast(line)  ///
		legend(off) ///
		scheme(s2mono) graphregion(fcolor(white)) ///
		xtitle(# Existing Sanctions Against Country) ///
		ytitle(Predicted Change in Stock Market) ///
		title("") 
		
* fn 11: Interact sanctions with exchange rates to ensure that the effect of sanctions is not conditional on the value of exchange rate. 
	xtreg StMk lStMk c.ST##c.EXRat c.ST2##c.EXRat c.ST3##c.EXRat IntCon CW prelec ideoLef openk lnTR  CPI lnGDP i.ccode i.yrmo ///
	,  fe vce(cluster ccode)
	
* Supplemental Appendix
	*Table SA1a: Summary Statistics for All Observations
	*Table SA1b: Sample Specific Summary Statistics for Models with Full Set of Control Variables
		*  Minimal Controls
			reg StMk lStMk ST ST2 ST3, vce(robust)
			
			estpost sum StMk dStMk ST AnyIMPTty AnyExpTty AnyFinSanTty SG20T  ///
			CPI lnGDP EXRat openk lnTR IntCon CW prelec ideoLef Dev_Cou ///
			if e(sample)==1 

	*  With Full Controls 
			xtreg StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk  ///
			lnTR EXRat CPI lnGDP i.yrmo, fe vce(cluster ccode)
				
			estpost sum StMk dStMk ST AnyIMPTty AnyExpTty AnyFinSanTty SG20T  ///
			CPI lnGDP EXRat openk lnTR IntCon CW prelec ideoLef Dev_Cou ///
			if e(sample)==1 
			
	*Table SA3a: Table 1 With Panel Corrected Standard Errors and a Common AR1
		* With Controls
			xtpcse StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP Dev_Cou, correlation(ar1) pairwise
					
		* With Minimal Controls
			xtpcse StMk lStMk ST ST2 ST3 CPI lnGDP Dev_Cou, correlation(ar1) pairwise
				
	*Table SA3b: Table 1 With Panel Corrected Standard Errors and a Panel Specific AR1
		* With Controls
			xtpcse StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP Dev_Cou, correlation(psar1) pairwise
	
		* With Minimal Controls
			xtpcse StMk lStMk ST ST2 ST3 CPI lnGDP Dev_Cou, correlation(psar1) pairwise
			
	*Table SA4: Table 1 With Multilevel Model and Random Coefficients for Sanctions
		* With Controls
			mixed StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk lnTR EXRat CPI lnGDP Dev_Cou ///
			||ccode:ST ST2 ST3, vce(robust) residuals(ar1,t(time)) mle
		
		* Minimal Controls
			mixed StMk lStMk ST ST2 ST3 CPI lnGDP Dev_Cou ///
			||ccode:ST ST2 ST3, vce(robust) residuals(ar1,t(time)) mle
			
	*Table SA5: Table 1 With Random Effects and Robust Standard Errors
		* With Controls
			xtreg StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk  ///
			lnTR EXRat CPI lnGDP Dev_Cou,  cluster (ccode) 
		
		* Minimal Controls 
			xtreg StMk lStMk ST ST2 ST3 CPI lnGDP Dev_Cou,  cluster (ccode) 
				
	*Table SA6: Table 1 With a Population Averaged General Estimating Equation with Identity Link, AR1 Correlation, and Robust Standard Errors
		* With Controls
			xtgee StMk lStMk ST ST2 ST3 IntCon CW prelec ideoLef openk  ///
			lnTR EXRat CPI lnGDP Dev_Cou, link(identity) family(gaussian) ///
			corr(ar1) robust force vsquish
		
		* Minimal Controls 
			xtgee StMk lStMk ST ST2 ST3 CPI lnGDP Dev_Cou, ///
			link(identity) family(gaussian) corr(ar1) robust force vsquish

